home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Tool Chest / Testing & Debugging / Virtual User / Virtual User Current Release / Tutorial / Script4.vu < prev    next >
Encoding:
Text File  |  1998-06-04  |  4.8 KB  |  165 lines  |  [TEXT/MPS ]

  1. # **********************************************************************
  2. # File:                Script 4.vu
  3. #
  4. # Purpose:            to demonstrate task and library use 
  5. #                    in structured scripting
  6. #
  7. # Prerequisites:        This script assumes that DrawShapesVU is the
  8. #                    currently active application on the target computer.
  9. #
  10. # Note:                Abbreviations are now used for all descriptor traits.
  11. #                    For example, instead of absolute: just a: is
  12. #                    used.
  13. #
  14. # Written by:        David Gaxiola
  15. #
  16. # Copyright © 1992 by Apple Computer, Inc., all rights reserved.
  17. #
  18. # **********************************************************************
  19.  
  20. # Use the StandardDialogs.vulib external library.
  21. Libraries 'StandardDialogs.vulib'; # libraries declaration
  22.  
  23. # Begin task definitions. **********************************************
  24.  
  25. # Tasks can have default values with their arguments.
  26. # Global variables must be marked as such, with the keyword global 
  27. # or they will be treated as local variables.
  28.  
  29. # **********************************************************************
  30. # Calculate the absolute difference between two numbers.
  31. task Diff( Num1, Num2 ) 
  32. begin
  33.     theDifference := Num1 - Num2;
  34.     if (theDifference < 0) do
  35.         theDifference := -theDifference;
  36.     return theDifference;
  37. end;
  38.  
  39. # **********************************************************************
  40. # Select one of the available tools.
  41. task SelectTool( toolNum := 1, randomDraw := false ) 
  42. begin
  43.     global gWindowDim;
  44.     global gToolColumn;
  45.     global gToolRow;
  46.  
  47.     if ( randomDraw ) do
  48.         toolNum := Random( 2, 4 );
  49.     move a:{ (gWindowDim[1] + gToolColumn), 
  50.                     (gWindowDim[2] + gToolRow[toolNum]) };
  51.     click;
  52. end;
  53.  
  54. # **********************************************************************
  55. # Select one of the seven predefined colors available.
  56. task SelectColor(colorNum := 1, randomColor := false) begin
  57.     global gColorAvail;
  58.     
  59.     if ( gColorAvail ) do 
  60.     begin
  61.         if randomColor do
  62.             colorNum := Random( 1, 7 );
  63.         select [ menuItem o:colorNum m:[ menu t:'Colors' ]];
  64.     end;
  65. end;
  66.  
  67. # **********************************************************************
  68. # Draw a rectangle with the cursor, remembering that a minimum size is
  69. # needed.
  70. task DrawRandomShapeWithTool( ) 
  71. begin
  72.     global gWindowDim;
  73.     global gWindowMod;
  74.     
  75.     range1 := ( gWindowDim[1] + gWindowMod[1] );
  76.     range2 := ( gWindowDim[2] + gWindowMod[2] );
  77.     range3 := ( gWindowDim[3] + gWindowMod[3] );
  78.     range4 := ( gWindowDim[4] + gWindowMod[4] );
  79.     xStart := Random( range1, range3 );
  80.     yStart := Random( range2, range4 );
  81.     xStop := Random( range1, range3 );
  82.     yStop := Random( range2, range4 );
  83.  
  84. # DrawShapesVU needs a minimum size in order to draw a shape.
  85. # These lines compensate for this need by making the difference
  86. # between the starting and stopping positions a minimum of 40.
  87.     
  88.     while ( Diff( xStart, xStop) < 40 ) 
  89.     begin
  90.         xStart := Random( range1, range3 );
  91.         xStop := Random( range1, range3 );
  92.     end;
  93.     
  94.     while ( Diff( yStart, yStop ) < 40 ) 
  95.     begin
  96.         yStart := Random( range2, range4 );
  97.         yStop := Random( range2, range4 );    
  98.     end;
  99.  
  100.     move a:{ xStart, yStart }; 
  101.     pressMouse;
  102.     Wait(1);                 # for slower targets
  103.     move a:{ xStop, yStop };  
  104.     releaseMouse;
  105. end;    
  106.  
  107. # Begin main body. ***********************************************
  108. script Tutorial4Main(maxIteration := 4, 
  109.                         saveAsFilename := 'Tutorial 4 Example.ds')
  110. begin
  111.  
  112. # global variable definitions *************************************
  113.     global gToolColumn := 18;
  114.     global gToolRow := { 40, 80, 120, 160 };
  115.     global gWindowMod := { 41, 21, -21, -21 };
  116.     global gWindowDim := { };
  117.     global gScreenDim := { };
  118.     global gColorAvail := false;
  119.  
  120.     MouseSpeed( 18 );
  121.     Patience( 4 );
  122.     match [ screen r:?gScreenDim m:true ];
  123.  
  124. # Check to see if the Color menu is available.
  125. # Here gColorAvail is first a string, but is transformed into a boolean.
  126.     match[ menu t:?gColorAvail o:5 ];
  127.     if ( gColorAvail = 'Colors' ) do
  128.     begin
  129.         gColorAvail := true;
  130.     end;
  131.     else 
  132.     begin
  133.         gColorAvail := false;
  134.     end;
  135.  
  136. # Create a new document and set it into position.
  137.     println "Setting up new DrawShapes document.";    
  138.     select [ menuItem t:'New' m:[ menu t:'File' ]];
  139.     drag [ window t:/Untitled-≈/ o:1 ] a:{ 1, 21 };
  140.     size [ window t:/Untitled-≈/ o:1 ] w:(gScreenDim[3] - 2) h:(gScreenDim[4] - 22);
  141.     match [ window r:?gWindowDim o:1 ];
  142.  
  143. # Perform a while...do loop instead of a for loop.
  144. # The result is the same.  Note the cleanliness because
  145. # of task usage.
  146.     currentIteration := 1;
  147.     while ( currentIteration <= maxIterations ) do     
  148.     begin
  149.         println "Drawing iteration {currentIteration}.";
  150.         SelectTool( 1, true );
  151.         DrawRandomShapeWithTool( );
  152.         SelectColor( 1, true );
  153.         currentIteration := currentIteration + 1;
  154.     end;
  155.     SelectTool( 3 );
  156.     DrawRandomShapeWithTool( );
  157.     SelectColor( 4 );
  158.  
  159. # Use the task DoSaveAs from the library StandardDialogs.vulib
  160.     println "Saving and closing window.";
  161.     DoSaveAs( saveAsFilename );
  162.     close [ window t:saveAsFilename o:1 ]!;
  163. end;
  164. # End Main execution. *************************************************
  165.